// pages/my/my.js
import api from '../../utils/api';
import Toast from '@vant/weapp/toast/toast';
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        carouselList: [
                    'http://xun.521xun.top/sl.png',
                    'http://xun.521xun.top/nl.jpg',
                    'http://xun.521xun.top/god/hutao.jpg',
                    'http://xun.521xun.top/god/hutao2.jpg'
                        ],
        carouselindex: 0,
        Ctab: null,
        userData: null,
        fileList: [],
        name: null,
        email: null,
        is_changeUser: false,
        code: null,
        nic: null,
        mail: null,
        is_info: false,
        huoCode: '获取验证码',
        nickName: null,
        avatarUrl: null,
        is_help:false,
        totalGrow:0,
        bate:0
    },
    infoShowTap() {
        this.setData({
            is_info: this.data.is_info ? false : true
        })
    },
    helpTap(){
        this.setData({
            is_help: this.data.is_help ? false : true
        })
    },
    nicChange(e) {
        this.setData({
            name: e.detail
        })
    },
    mailChange(e) {
        this.setData({
            email: e.detail
        })
    },
    codeChange(e) {
        this.setData({
            code: e.detail
        })
    },
    upInfoClose() {
        this.setData({
            name: null,
            email: null,
            code: null,
            is_changeUser: false
        })
    },
    userupdateTap() {
        console.log(11)
        this.setData({
            is_changeUser: true
        })
    },
    codeTap() {
        let email = {
            mail: this.data.email
        }
        let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{3,4})$/;
        if (!email.mail) {
            Toast('请输入邮箱~');
        } else {
            if (!reg.test(email.mail)) {
                Toast('请输入正确QQ邮箱~');
            } else {
                if (this.data.huoCode == '获取验证码') {
                    console.log('获取验证码')
                    let Codetime = 60
                    let codebtnSpanTime = setInterval(() => {
                        console.log(Codetime)
                        Codetime--
                        this.setData({
                            huoCode: Codetime
                        })
                        if (Codetime == 0) {
                            this.setData({
                                huoCode: '获取验证码'
                            })
                        }
                    }, 1000)
                    api.postReguserCode(email).then((res) => {
                        console.log(res)
                        Toast('已发送，五分钟有效~')
                        clearInterval(codebtnSpanTime);
                    })
                }

            }
        }
    },
    userTap() {
        console.log('修改个人信息')
        let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{3,4})$/;
        let name = this.data.name
        let email = this.data.email
        let vname, vmail = null
        if (name) {
            if (name.length < 1 || name.length > 10) {
                Toast('请输入1至10位昵称~');
            } else {
                vname = {
                    name: name
                }
            }
        }

        if (email) {
            if (!reg.test(email)) {
                Toast('请输入正确QQ邮箱~');
            } else {
                vmail = {
                    email: email,
                    code: this.data.code
                }
            }
        }
        let value = {
            ...value,
            ...vname,
            ...vmail
        }
        if (vname||vmail) {
            api.updateUserInfo(value).then((res) => {
                console.log(value)
                if (res.status == '0') {
                    Toast('修改成功~');
                    if (name) {
                        this.setData({
                            ['userData.name']: this.data.name,
                        })
                        app.globalData.userInfo.name = this.data.name
                    }
                    
                    if (email) {
                        this.setData({
                            ['userData.email']: this.data.email,
                        })
                        app.globalData.userInfo.email = this.data.email
                    }
                    this.upInfoClose()
                } else {
                    Toast('修改失败！');
                    this.upInfoClose()
                }
            })
        } else {
            Toast('请至少填写一项~');
        }


    },
    getUserProfile() {
        let that = this
        wx.getUserProfile({
            desc: '获取头像和昵称', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                this.setData({
                    nickName: res.userInfo.nickName,
                    avatarUrl: res.userInfo.avatarUrl
                })
                wx.setStorage({ //数据缓存方法
                    key: 'nickName', //关键字，本地缓存中指定的key
                    data: res.userInfo.nickName, //缓存微信用户公开信息，
                    success: function () { //缓存成功后，输出提示
                        console.log('写入nickName缓存成功')
                        app.globalData.userInfo.name=wx.getStorageSync('nickName')
                        that.setData({
                            userData: app.globalData.userInfo
                        })
                    },
                    fail: function () { //缓存失败后的提示
                        console.log('写入nickName发生错误')
                        Toast('写入昵称出错~');
                    }
                })
                wx.setStorage({ //数据缓存方法
                    key: 'avatarUrl', //关键字，本地缓存中指定的key
                    data: res.userInfo.avatarUrl, //缓存微信用户公开信息，
                    success: function () { //缓存成功后，输出提示
                        console.log('写入avatarUrl缓存成功')
                        app.globalData.userInfo.headPortrait=wx.getStorageSync('avatarUrl')
                        that.setData({
                            userData: app.globalData.userInfo
                        })
                        // 掉用接口
                        let value = {
                            name: wx.getStorageSync('nickName'),
                            headPortrait: wx.getStorageSync('avatarUrl')
                        }
                        api.updateUserInfo(value).then((res) => {
                            console.log(value)
                            if (res.status == '0') {
                                Toast('修改成功~');
                            } else {
                                Toast('修改失败！');
                            }
                        })
                    },
                    fail: function () { //缓存失败后的提示
                        console.log('写入avatarUrl发生错误')
                        Toast('写入头像出错~');
                    }
                })
            }
        })
    },
    afterRead(event) {
        const url = {
            url: event.detail.file.url
        };
        console.log(event.detail)
        let fl = 'fileList[' + (0) + ']'
        this.setData({
            [fl]: url
        })
        console.log(this.data.fileList)

    },
    deleteImg() {
        this.setData({
            fileList: null
        })
    },

    carouselFun() {
        let i = 0
        setInterval(() => {
            i++
            if (i == this.data.carouselList.length) {
                i = 0
            }
            this.setData({
                Ctab: 'cTabO'
            })
            setTimeout(() => {
                this.setData({
                    Ctab: 'cTabH',
                    carouselindex: i
                })
            }, 1000)
        }, 5000);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.carouselFun()
        this.setData({
            userData: app.globalData.userInfo,
            nic: app.globalData.userInfo.name,
            mail: app.globalData.userInfo.email,
            
        })
        console.log(this.data.userData.growthValue)
        this.setData({
            totalGrow:parseInt(this.data.userData.growthValue)+parseInt(this.data.userData.currency),
            bate:parseInt((parseInt(this.data.userData.growthValue)+parseInt(this.data.userData.currency))/parseInt(this.data.userData.currency)) ,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})